package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.dto.RecruitPageDto;
import com.woniu.model.form.*;
import com.woniu.model.model.YRecruit;
import com.woniu.model.param.AddRecruitParam;
import com.woniu.model.param.PageParam;
import com.woniu.model.param.UpdateRecruitParam;
import com.woniu.model.param.VenueLoadMyRecruitParam;
import com.woniu.service.YRecruitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author EdisonLiu
 * @since 2024年05月23日
 */
@RestController
@RequestMapping("/yRecruit")
@Api(tags = "招聘信息接口")
public class YRecruitController {

    @Resource
    private YRecruitService recruitService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 新增招聘信息
     * @param form
     * @param token token取venueId
     * @return
     */
    @PostMapping("/addRecruit")
    @ApiOperation(value = "新增招聘信息")
    public Result<?> addRecruit(@RequestBody AddRecruitForm form, @RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());
        AddRecruitParam p = BeanUtil.toBean(form, AddRecruitParam.class);
        p.setVenueId(venueId);

        Integer i =recruitService.addRecruit(p);
        if (!i.equals(1)){
            return Result.getResult().setMsg("新增招聘信息失败").setCode(500);
        }
        return Result.getResult().setMsg("新增招聘信息成功");
    }

    /**
     * 修改招聘信息
     * @param form 更改后的招聘信息
     * @return
     */
    @PostMapping("/updateRecruit")
    @ApiOperation(value = "修改招聘信息")
    public Result<?> updateRecruit(@RequestBody UpdateRecruitForm form, @RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("id").toString());
        UpdateRecruitParam p = BeanUtil.toBean(form, UpdateRecruitParam.class);
        p.setVenueId(venueId);

        Integer i =recruitService.updateRecruit(p);
        if (!i.equals(1)){
            return Result.getResult().setMsg("修改招聘信息失败").setCode(500);
        }
        return Result.getResult().setMsg("修改招聘信息成功");
    }

    /**
     * 删除招聘信息
     * @param recruitId 招聘信息id
     * @param token token取出登录信息
     * @return
     */
    @GetMapping("/cancelRecruit")
    @ApiOperation(value = "删除招聘信息")
    public Result<?> cancelRecruit(Integer recruitId, @RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());

        Integer i =recruitService.cancelRecruit(recruitId,venueId);
        if (!i.equals(1)){
            return Result.getResult().setMsg("删除招聘信息失败").setCode(500);
        }
        return Result.getResult().setMsg("删除招聘信息成功");
    }

    /**
     * 分页查询所有招聘信息
     * @param form
     * @return
     */
    @GetMapping("/pageRecruit")
    @ApiOperation(value = "分页查询招聘信息")
    public Result<Page> pageRecruit(PageRecruitForm form){
        Page<YRecruit> i = recruitService.pageRecruit(form);
        return Result.newInstance(Page.class).setMsg("查询所有招聘信息成功").setData(i);
    }
    /**
     * 分页查询所有招聘信息
     * @param form
     * @return
     */
    @GetMapping("/pageAllRecruit")
    @ApiOperation(value = "分页查询所有场馆招聘信息")
    public Result<Page<RecruitPageDto>> pageAllRecruit(PageForm form){
        Page<RecruitPageDto> i = recruitService.pageAllRecruit(BeanUtil.toBean(form, PageParam.class));
        return Result.newPageInstance(RecruitPageDto.class).setMsg("查询所有招聘信息成功").setData(i);
    }




    /**
     * 场馆端查询自己发布了的招聘信息（+带条件搜索）
     * @param form
     * @return
     */
    @GetMapping("/VenueLoadMyRecruit")
    @ApiOperation(value = "场馆端查询自己发布了的招聘信息（+带条件搜索）")
    public Result<Page<RecruitPageDto>> VenueLoadMyRecruit(VenueLoadMyRecruitForm form, @RequestHeader String token){
        String refreshToken = redisUtil.get(token);
        Integer venueId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("venueId").toString());
        VenueLoadMyRecruitParam param = BeanUtil.toBean(form, VenueLoadMyRecruitParam.class);
        param.setVenueId(venueId);

        Page<RecruitPageDto> i = recruitService.VenueLoadMyRecruit(param);
        return Result.newPageInstance(RecruitPageDto.class).setMsg("查询所有招聘信息成功").setData(i);
    }

}

